<?php

namespace App\Http\Controllers\Finance;

use App\Common\Traits\Customer\CustomerTrait;
use App\Http\Controllers\Controller;
use App\Models\CustomerManager;
use App\Models\ShopDetail;
use App\Models\SubAccount\SubAccount;
use Illuminate\Http\Request;
use App\Jobs\Finance\StopCustomerBillJob;

class FinanceStopCustomerBillController extends Controller
{
    use CustomerTrait;

    //终止客户账单
    public function stopCustomerBill(Request $request){
        $customer=CustomerManager::find($request->customer_id??0);
        if(empty($customer)){
            return $this->failed('客户id错误，无此客户',201);
        }
        if($customer->is_bill_stopped==1){
            return $this->failed('终止失败，客户账单已终止过',201);
        }
        //生成账单
        $this->createCustomerBill($customer);

        $this->updateCustomerData($customer);
        $this->disableAllSubAccounts($customer);
        return $this->success('客户账单已中止');
    }
    //更新客户数据
    public function updateCustomerData($customer){
        $customer->is_bill_stopped=1;
        return $customer->save();
    }
    //生成账单
    public function createCustomerBill($customer){
        StopCustomerBillJob::dispatch($customer);
    }



}
